<template>
  <!--面包屑-->
  <div style="margin-bottom: 30px">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/Main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <router-link to="/Approval/MaterialProcurementShow">物资采购审批</router-link>
      </el-breadcrumb-item>
      <el-breadcrumb-item>物资采购审批列表</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  
  
  <!--查询条件-->
  <div style="margin-bottom: 30px;">
    物资名称：<el-input v-model="form.nameOfGoods" placeholder="请输入货物名称" style="width: 240px;"></el-input>&nbsp;&nbsp;
    材质：<el-input v-model="form.material" placeholder="请输入材质" style="width: 240px;"></el-input>&nbsp;&nbsp;
    产地<el-select v-model="form.CountryId" placeholder="请选择国家" size="large" style="width: 240px" @change="getEconomizeList">
        <el-option v-for="item in countryData" :key="item.countryId" :label="item.countryName" :value="item.countryId"/>
      </el-select>
          
          &nbsp;&nbsp;
          <el-select v-model="form.EconomizeId" placeholder="请选择省/州" size="large" style="width: 240px">
        <el-option v-for="item in EconomizeData" :key="item.economizeId" :label="item.economizeName" :value="item.economizeId"/>
      </el-select>
    <br/>
    <br/>
    申请人：<el-input v-model="form.applicant" placeholder="请输入申请人" style="width: 240px;"></el-input>&nbsp;&nbsp;
    审批状态：
    <el-select v-model="form.approvalStatus" placeholder="请选择" style="width: 240px;">
      <el-option label="全部" value="" />
      <el-option v-for="item in ApprovalStatusData" :key="item.enumId" :label="item.enumName" :value="item.enumId" />
    </el-select> &nbsp;&nbsp; &nbsp;&nbsp; 
  
    <el-button type="primary" @click="getMaterialsList">查询</el-button>
    <el-button @click="reset">重置</el-button>
    <br/>
    <br/>
    <el-tabs v-model="form.ApprovalStatus" class="demo-tabs" @click="handleClick(form.ApprovalStatus)">
    <el-tab-pane label="全部" name=""></el-tab-pane>
    <!-- <el-tab-pane v-for="item in ApprovalStatusData" :key="item.enumId" :label="item.enumName" :name="item.enumId" ></el-tab-pane> -->
    <el-tab-pane label="待审批" :name="PendingApproval.enumId"></el-tab-pane>
    <el-tab-pane label="已审批" :name="PassedStatus.enumId"></el-tab-pane>
    <!-- <el-tab-pane :label="PendingApproval.enumName" :name="PendingApproval.enumId"></el-tab-pane> -->
  </el-tabs>
  </div>
  
  <!--物资列表-->
  <div>
    <el-table :data="MaterialsData" border style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" />
      <el-table-column prop="nameOfGoods" label="货物名称" align="center" />
      <el-table-column prop="materialStoreType" label="类型" align="center" width="90px" />
      <el-table-column prop="material" label="材质" align="center" />
      <el-table-column prop="specifications" label="规格" align="center" />
      <el-table-column label="产地" align="center" width="180px" >
        <template v-slot="scope">
          {{ scope.row.countryName }} -- {{scope.row.economizeName}}
        </template>
      </el-table-column>
      <el-table-column prop="number" label="数量" align="center" />
      <el-table-column prop="usageDescription" label="用途描述" align="center" width="140px" />
      <el-table-column prop="applicant" label="申请人" align="center" />
      <el-table-column prop="deliveryDate" label="期望交付日期" align="center" width="135px" >
        <template v-slot="scope">
          {{ scope.row.deliveryDate.substr(0,10) }}
        </template>
      </el-table-column>
      <el-table-column prop="notes" label="备注" align="center" />
      <el-table-column prop="createdDate" label="创建时间" align="center" width="135px" >
        <template v-slot="scope">
          {{ scope.row.createdDate.substr(0,16).replace("T"," ") }}
        </template>
      </el-table-column>
      <el-table-column prop="approvalStatus" label="状态" align="center" >
        <template v-slot="scope">
          <el-tag type="primary" v-if="scope.row.approvalStatus==10">待提交</el-tag>
          <el-tag type="warning" v-if="scope.row.approvalStatus==20">待审批</el-tag>
          <el-tag type="success" v-if="scope.row.approvalStatus==30">已通过</el-tag>
          <el-tag type="danger" v-if="scope.row.approvalStatus==40">已拒绝</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="approver" label="审批人" align="center" />
      <el-table-column label="操作" align="center" width="202px" >
        <template v-slot="scope">
          <el-link type="primary" target="_blank" @click="lockUp(scope.row.materialStoreId)" >查看</el-link>&nbsp;&nbsp;
          <el-link type="primary" target="_blank" :disabled="scope.row.approvalStatus == 30 || scope.row.approvalStatus == 40"  @click="MaterialApprovalCommand(scope.row.materialStoreId,30)" >通过</el-link>&nbsp;&nbsp;
          <el-link type="primary" target="_blank" :disabled="scope.row.approvalStatus == 30 || scope.row.approvalStatus == 40"  @click="MaterialApprovalCommand(scope.row.materialStoreId,40)" >拒绝</el-link>&nbsp;&nbsp;
        </template>
      </el-table-column>
    </el-table>
  </div>
  
  <!--分页-->
  <div>
    <table>
    <thead></thead>
    <tbody>
      <tr>
        <td>
          <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize" :page-sizes="[3, 5, 7, 9]" :background="true" layout="total,slot, sizes" :total="form.totalCount" prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            <span>
              ，每页{{ form.pageSize }}条，当前第:{{ form.pageIndex }}/{{ form.pageCount }}页
            </span>
          </el-pagination>
        </td>
  
        <td>
          <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize" :page-sizes="[100, 200, 300, 400]" :background="true" layout="slot, prev, pager, next" :total="form.totalCount" prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            <el-button type="primary" class="btn-prev" @click="handleCurrentChange(1)" :disabled="form.pageIndex==1">首页</el-button>
          </el-pagination>
        </td>
  
        <td>
          <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize" :page-sizes="[100, 200, 300, 400]" :background="true" layout="slot, jumper" :total="form.totalCount" prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            <el-button type="primary" class="btn-prev" @click="handleCurrentChange(form.pageCount)" :disabled="form.pageIndex==form.pageCount">尾页</el-button>
          </el-pagination>
        </td>
      </tr>
    </tbody>
  </table>
  </div>
  
  </template>
  
  <script setup lang="ts">
  import { reactive, ref, onMounted, computed } from 'vue'
  import axios from 'axios';
  import type { ComponentSize, FormInstance, FormRules,TabsPaneContext } from 'element-plus'
  import { ElMessage, ElMessageBox } from 'element-plus'

  import {useRoute,useRouter} from 'vue-router'
  
  const route = useRoute();
  const router = useRouter();
  
  onMounted(() => {
    //物资列表
    getMaterialsList()
  
    //审批枚举状态
    getApprovalStatus()
  
    getCountryList()
    getEconomizeList()

  })
  
  const form: any = reactive({
    nameOfGoods: '',
    material: '',
      CountryId: '',
      EconomizeId: '',
    applicant: '',
    approvalStatus: '',
    pageIndex: 1,
    pageSize: 15,
    buttonArray: [],
  })

const handleClick = (ApprovalStatus:number) => {
  console.log(ApprovalStatus)
  form.approvalStatus = ApprovalStatus;
  getMaterialsList()
}
  
      //获取国家列表
      const countryData: any = ref([]);
    const getCountryList = () => {
      axios.get('/mread/api/ShowMaterialStore/GetCountryList', {
        params: {
          
        }
      }).then(res=>{
      //console.log(res);
        countryData.value = res.data.data;
    }).catch(err=>{
      console.log(err);
      
    })    
    }
  
      //获取省/州列表
      const EconomizeData: any = ref([]);
    const getEconomizeList = () => {
       form.EconomizeId = "";
      axios.get('/mread/api/ShowMaterialStore/GetEconomizeList', {
        params: {
          CountryId: form.CountryId || 0
        }
      }).then(res=>{
      //console.log(res);
      
      EconomizeData.value = res.data.data;
    }).catch(err=>{
      console.log(err);
      
    })    
    }
  
  //存储多选中的物资id
  const multipleSelection:any = ref([]);
  const handleSelectionChange = (val:any) => {
    multipleSelection.value = val;
  }
  
  //获取审批状态枚举反射
  const ApprovalStatusData: any = ref([])
  //待审批
  const PendingApproval:any=ref('')
  //已通过
  const PassedStatus:any=ref('')
  const getApprovalStatus=()=>{
    axios.get('/mread/api/ShowMaterialStore/GetApprovalStatusEnum').then(res=>{
      //console.log(res);
      ApprovalStatusData.value = res.data;
      PendingApproval.value = res.data[1]
      PassedStatus.value = res.data[2]
      //console.log(PendingApproval.value);
      
    }).catch(err=>{
      console.log(err);
      
    })    
  }
  
  
  //分页
  const handleSizeChange = (val: number) => {
    form.pageSize = val;
    getMaterialsList()
  }
  const handleCurrentChange = (val: number) => {
    form.pageIndex = val;
    getMaterialsList()
  }
  
  //物资列表
  const MaterialsData:any=ref([])
  const getMaterialsList = () => {
  
    axios.get('/mread/api/ExamineAndApprove/GetExamineAndApproveList', {
      params: {
        NameOfGoods: form.nameOfGoods,
        Material: form.material,
        CountryId: form.CountryId,
        EconomizeId: form.EconomizeId,
        Applicant: form.applicant,
        ApprovalStatus: form.approvalStatus,
        pageIndex: form.pageIndex,
        pageSize: form.pageSize,
      }
    }).then(res=>{
      //console.log(res);
      MaterialsData.value = res.data.data.list;
      form.pageCount = res.data.data.pageCount;
      form.totalCount = res.data.data.totalCount;
    }).catch(err=>{
      console.log(err);
      
    })    
  }
  
  //重置
  const reset = () => {
    form.nameOfGoods = '',
    form.material = '',
    form.CountryId='',
    form.EconomizeId= '',
    form.applicant = '',
    form.approvalStatus = '',
    form.pageIndex = 1,
    form.pageSize = 5,
    getMaterialsList()
  }

  //修改状态--通过/不通过
  const MaterialApprovalCommand = (materialStoreId: number, approvalStatus: number) => {
    axios.post('/mwrite/api/MaterialStore/UpdateMaterialApprova', {
      materialStoreId: materialStoreId,
      materialClassificat:1,
      approvalStatus: approvalStatus,
    }).then(res => {
      console.log(res);
      if (res.data.code > 0) {
        ElMessage.success("审批成功!")
        //刷新页面
        getMaterialsList()
      } else {
        ElMessage.error(res.data.message)
        return
      }
    }).catch(err => {
      console.log(err);

    })
  }

  //查看
  const lockUp = (materialStoreId: number) => {
    axios.get('/mread/api/ShowMaterialStore/GetMaterialStoreByIdList', {
      params: {
        MaterialStoreId: materialStoreId
      }
    }).then(res => {
      console.log(res);
      
      form.approvalStatus = res.data.data[0].approvalStatus
      //已审批
      if (form.approvalStatus == 30 || form.approvalStatus == 40) {
        router.push({
          path: '/Approval/MaterialApprovedLock',
          query: {
            materialStoreId: materialStoreId
           }

        })
      }
      else {
        //未审批
        router.push({
          path: '/Approval/PendingApproval',
          query: { 
            materialStoreId: materialStoreId
           }

        })
      }
    })
  }

  </script>
  
  <style scoped>
  
  .demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

  </style>